

<template>
  <div>
    <div class="register-wrapper">
      <div id="register">
        <p class="title">注册</p>
        <el-form
            :model="ruleForm2"
            status-icon
            :rules="ruleForm2Rules"
            ref="ruleForm2"
            label-width="0"
            class="demo-ruleForm"
        >

          <el-form-item prop="name">
            <el-input v-model="ruleForm2.name" auto-complete="off" placeholder="用户名"></el-input>
            </el-form-item>

          <el-form-item prop="pass">
            <el-input type="password" v-model="ruleForm2.pass"
                      auto-complete="off" placeholder="输入密码"></el-input>
          </el-form-item>

          <el-form-item prop="checkPass">
            <el-input type="password" v-model="ruleForm2.checkPass"
                      auto-complete="off" placeholder="确认密码"
            @blur="validatePass2()"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button @click="flag = !flag">是否要设置密钥</el-button>
          </el-form-item>
          <template  v-if="flag">
            <el-form-item prop="secretKey">
            <el-input v-model="ruleForm2.secretKey" auto-complete="off" placeholder="输入密钥"></el-input>
          </el-form-item>

            <el-form-item prop="checkSecretKey  ">
              <el-input   type="password"  v-model="ruleForm2.checkSecretKey"
                          auto-complete="off" placeholder="确认密钥"
                          @blur="validateCheckSecretKey()"></el-input>
            </el-form-item>
          </template>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm2')"
                       style="width:100%;"  :disabled="ref" >注册</el-button>
            <p class="login" @click="gotoLogin">已有账号？立即登录</p>
          </el-form-item>

        </el-form>
      </div>
    </div>
  </div>
</template>



<script>
import  {regUser} from  "@/api/reg.js"
export default {
  name: "Register",
  data() {

    return {
      flag: false ,
      ref:false ,
      ruleForm2: {
        pass: "",
        checkPass: "",
        name: "",
        secretKey:"",
        checkSecretKey:""
      },
      ruleForm2Rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' } ,
          { min: 6, max: 20, message: '用户名长度应在 6 到 20 个字符之间', trigger: 'blur' }
        ],
        pass: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '密码长度应在 6 到 20 个字符之间', trigger: 'blur' }
        ],
        checkPass: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        secretKey: [
          { required: true, message: '请输入密钥', trigger: 'blur' }
        ],
        checkSecretKey: [
          { required: true, message: '请确认密钥', trigger: 'blur' },
        ]
      },
      isDisabled: false, // 是否禁止点击发送验证码按钮

    }
  },
  methods: {

    validateCheckSecretKey(){
      if(this.ruleForm2.secretKey != this.ruleForm2.checkSecretKey){
        this.$message.error('密钥不一致');
        this.ref = true ;
        return false;
      }
      this.ref = false ;
    },
    validatePass2(){
      if(this.ruleForm2.pass != this.ruleForm2.checkPass){
        this.$message.error('密码不一致');
        this.ref = true ;
        return false;
      }
      this.ref = false ;
    },
    // <!--提交注册-->
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {

          regUser(this.ruleForm2).then(res => {
             let data = res.data;
             console.log(data)
            if (data.code != 200){
              this.$message.error(data.msg);
            }else {
              this.$message.success('注册成功！');
              this.$router.push({
                path: "/login"
              });
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      })
    },
    // <!--进入登录页-->
    gotoLogin() {
      this.$router.push({
        path: "/login"
      });
    },

  }
};
</script>

<style scoped>
.loading-wrapper {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  background: #aedff8;
  display: flex;
  align-items: center;
  justify-content: center;
}
.register-wrapper img {
  position: absolute;
  z-index: 1;
}
.register-wrapper {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
}
#register {
  max-width: 340px;
  margin: 60px auto;
  background: #fff;
  padding: 20px 40px;
  border-radius: 10px;
  position: relative;
  z-index: 9;
}
.title {
  font-size: 26px;
  line-height: 50px;
  font-weight: bold;
  margin: 10px;
  text-align: center;
}
.el-form-item {
  text-align: center;
}
.login {
  margin-top: 10px;
  font-size: 14px;
  line-height: 22px;
  color: #1ab2ff;
  cursor: pointer;
  text-align: left;
  text-indent: 8px;
  width: 160px;
}
.login:hover {
  color: #2c2fd6;
}
.code >>> .el-form-item__content {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.code button {
  margin-left: 20px;
  width: 140px;
  text-align: center;
}
.el-button--primary:focus {
  background: #409EFF;
  border-color: #409EFF;
  color: #fff;
}
</style>




